package com.triones.api.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.triones.api.entity.CountryDictDO;
import com.triones.api.entity.HelpCategorizeDO;
import com.triones.api.entity.HelpDO;
import com.triones.api.entity.vo.HelpCategorizeVO;
import com.triones.api.entity.vo.HelpVO;
import com.triones.api.mapper.HelpCategorizeMapper;
import com.triones.api.mapper.HelpMapper;
import com.triones.api.service.HelpService;
import io.netty.util.internal.StringUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 帮助中心 -业务实现
 */
@Service
@RequiredArgsConstructor
public class HelpServiceImpl implements HelpService {

    private final HelpMapper helpMapper;

    private final HelpCategorizeMapper helpCategorizeMapper;

    @Override
    public List<HelpCategorizeVO> apiCategorizeList() {
        return helpCategorizeMapper.selectVoList(new LambdaQueryWrapper<HelpCategorizeDO>().eq(HelpCategorizeDO::getDel, "0")
            .orderByAsc(HelpCategorizeDO::getSort));
    }

    @Override
    public List<HelpVO> apiHelpList(Long cid, String keyword) {
        return helpMapper.selectVoList(new LambdaQueryWrapper<HelpDO>().eq(HelpDO::getCategorizeId, cid)
            .and(ObjectUtil.isNotEmpty(keyword),
                wq  -> wq.like(HelpDO::getZhTitle, keyword).or()
                .like(HelpDO::getEnTitle, keyword).or()
                .like(HelpDO::getJaTitle, keyword).or()
                .like(HelpDO::getZhContent, keyword).or()
                .like(HelpDO::getEnContent, keyword).or()
                .like(HelpDO::getJaContent, keyword)
            )
            .orderByAsc(HelpDO::getSort));
    }

    @Override
    public TableDataInfo<HelpCategorizeVO> selectPageHelpCategorizeList(HelpCategorizeDO dto, PageQuery pageQuery) {
        Map<String, Object> params = dto.getParams();
        LambdaQueryWrapper<HelpCategorizeDO> lqw = new LambdaQueryWrapper<HelpCategorizeDO>()
            .eq(HelpCategorizeDO::getDel,"0")
            .and(StringUtils.isNotBlank(dto.getZhCategorizeName()),
                wq  -> wq.like(HelpCategorizeDO::getZhCategorizeName, dto.getZhCategorizeName()).or()
                .like(HelpCategorizeDO::getEnCategorizeName, dto.getZhCategorizeName()).or()
                .like(HelpCategorizeDO::getJaCategorizeName, dto.getZhCategorizeName())
            )
            .eq(StringUtils.isNotBlank(dto.getStatus()), HelpCategorizeDO::getStatus, dto.getStatus())
            .between(params.get("beginTime") != null && params.get("endTime") != null,
                HelpCategorizeDO::getCreateAt, params.get("beginTime"), params.get("endTime"));
        Page<HelpCategorizeVO> page = helpCategorizeMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    @Override
    public HelpCategorizeVO selectHelpCategorizeById(Long id) {
        return helpCategorizeMapper.selectVoById(id);
    }

    @Override
    public boolean checkHelpCategorizeUnique(HelpCategorizeDO dto) {
        boolean exist = helpCategorizeMapper.exists(new LambdaQueryWrapper<HelpCategorizeDO>()
            .eq(HelpCategorizeDO::getZhCategorizeName, dto.getZhCategorizeName())
            .eq(HelpCategorizeDO::getDel,"0")
            .ne(ObjectUtil.isNotNull(dto.getId()), HelpCategorizeDO::getId, dto.getId()));
        return !exist;
    }

    @Override
    public void insertHelpCategorize(HelpCategorizeDO dto) {
        dto.setCreateAt(System.currentTimeMillis());
        helpCategorizeMapper.insert(dto);
    }

    @Override
    public void updateHelpCategorize(HelpCategorizeDO dto) {
        dto.setCreateAt(System.currentTimeMillis());
        helpCategorizeMapper.updateById(dto);
    }

    @Override
    public void deleteHelpCategorizeByIds(Long id) {
        HelpCategorizeDO categorizeDO = helpCategorizeMapper.selectById(id);
        if (categorizeDO == null) {
            throw new ServiceException("数据不存在");
        }
        List<HelpDO> helpDOS = helpMapper.selectList(new LambdaQueryWrapper<HelpDO>()
            .eq(HelpDO::getCategorizeId, id).eq(HelpDO::getDel, "0"));
        if (helpDOS.size() > 0) {
            throw new ServiceException("该分类下有问答不允许删除");
        }
        categorizeDO.setDel("1");
        helpCategorizeMapper.updateById(categorizeDO);
    }

    @Override
    public List<HelpCategorizeVO> selectHelpCategorizeAll() {
        return helpCategorizeMapper.selectVoList(null);
    }

    @Override
    public TableDataInfo<HelpVO> list(HelpDO dto, PageQuery pageQuery) {
        Map<String, Object> params = dto.getParams();
        LambdaQueryWrapper<HelpDO> lqw = new LambdaQueryWrapper<HelpDO>()
        .eq(HelpDO::getCategorizeId,dto.getCategorizeId())
            .and(StringUtils.isNotBlank(dto.getZhTitle()),
                w -> w.like(HelpDO::getZhTitle, dto.getZhTitle()).or()
                    .like(HelpDO::getEnTitle, dto.getZhTitle()).or()
                    .like(HelpDO::getJaTitle, dto.getZhTitle()).or()
                    .like(HelpDO::getZhContent, dto.getZhTitle()).or()
                    .like(HelpDO::getEnContent, dto.getZhTitle()).or()
                    .like(HelpDO::getJaContent, dto.getZhTitle())
            )
            .eq(HelpDO::getDel,"0")
            .eq(StringUtils.isNotBlank(dto.getStatus()), HelpDO::getStatus, dto.getStatus())
            .between(params.get("beginTime") != null && params.get("endTime") != null,
                HelpDO::getCreateTime, params.get("beginTime"), params.get("endTime"));
        Page<HelpVO> page = helpMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    @Override
    public HelpVO selectDataById(Long id) {
        return helpMapper.selectVoById(id);
    }

    @Override
    public void insertData(HelpDO dto) {
        dto.setCreateTime(System.currentTimeMillis());
        helpMapper.insert(dto);
    }

    @Override
    public void updateData(HelpDO dto) {
        helpMapper.updateById(dto);
    }

    @Override
    public void deleteDataByIds(Long id) {
        HelpDO helpDO = helpMapper.selectById(id);
        if (helpDO == null) {
            throw new ServiceException("数据不存在");
        }
        helpDO.setDel("1");
        helpMapper.updateById(helpDO);
    }

    @Override
    public boolean checkDataTitleUnique(HelpDO dto) {
        boolean exist = helpMapper.exists(new LambdaQueryWrapper<HelpDO>()
            .eq(HelpDO::getZhTitle, dto.getZhTitle())
            .eq(HelpDO::getDel,"0")
            .ne(ObjectUtil.isNotNull(dto.getId()), HelpDO::getId, dto.getId()));
        return !exist;
    }
}
